package com.alcatel_lucent.osp.action;

import com.alcatel_lucent.osp.model.Page;
import com.alcatel_lucent.osp.model.PageId;
import com.alcatel_lucent.osp.service.DaoService;
import com.alcatel_lucent.osp.service.JSONService;
import com.alcatel_lucent.osp.service.JSONServiceImpl;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: guoshunw
 * Date: 13-9-2
 * Time: 上午10:44
 * To change this template use File | Settings | File Templates.
 */


@Result(type = "chain", params = {"actionName", "${includePage}", "namespace", "/"})
public class GetIncludePageAction extends BaseAction implements SessionAware {

    private Long categoryId;
    private Long typeId;

    private String includePage;

    private DaoService dao;
    private Map<String, Object> session;

    @Autowired
    private JSONService jsonService;

    public Map<String, String> getComponentAndReleases() throws IOException {
        Map result = (Map) readJSONResource(JSONServiceImpl.COMPONENT_AND_RELEASE);
        return null != result ? result : MapUtils.EMPTY_MAP;
    }

    public Map<String, String> getDisableProjects() throws IOException {
        return(Map) readJSONResource(JSONServiceImpl.PROJECT_TO_DISABLE);
    }

    public Map<String, String> getChangeProjects() throws IOException {
        return (Map) readJSONResource(JSONServiceImpl.PWD_TO_LOCAL);
    }

    public List<String> getProjectManagers() throws IOException {
        return (List<String>) readJSONResource(JSONServiceImpl.PROJECT_MANAGER);
    }

    private Object readJSONResource(String resourceKey) throws IOException {
        Map<String, String> postData = new HashMap<String, String>();
        postData.put(JSONServiceImpl.RESOURCE_NAME_KEY, resourceKey);
        return jsonService.getJSONResource(postData);
    }


    @Autowired
    public void setDao(DaoService dao) {
        this.dao = dao;
    }

    public String getIncludePage() {
        return includePage;
    }

    public void setIncludePage(String includePage) {
        this.includePage = includePage;
    }

    public Long getTypeId() {
        return typeId;
    }

    public void setTypeId(Long typeId) {
        this.typeId = typeId;
    }

    public Long getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Long categoryId) {
        this.categoryId = categoryId;
    }

    public String execute() throws Exception {
        log.info("Category id ={}, type id={}", categoryId, typeId);
        Page page = new Page();
        if (null != categoryId && null != typeId) {
            Map<String, Object> params = new HashMap<String, Object>();

            params.put("id", new PageId(categoryId, typeId, "new"));

            Page queryPage = (Page) dao.retrieveOne("from Page where id=:id", params);
            if (null != queryPage) page = queryPage;
        }
        setIncludePage(page.getName());

        // clear temporary attachments in session
        session.remove(UploadAttachmentAction.UPLOAD_TOKEN_KEY);
        session.remove(UploadAttachmentAction.TRANSIENT_ATTACHMENT_ID_KEY);

        return SUCCESS;
    }

    @Override
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    public Collection<String> getPriorities() {
        return Arrays.asList("Blocker", "Critical", "Major", "Minor", "Trivial", "Unclassified");
    }

}
